package com.vxb.testmvp.test_zonghe;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.vxb.testmvp.R;
import com.vxb.testmvp.test_zonghe.test_1.TestMActivity;
import com.vxb.testmvp.test_zonghe.test_2.TestTowActivity;
import com.vxb.testmvp.test_zonghe.test_3.Test3Activity;
import com.vxb.testmvp.test_zonghe.test_4.Test4Activity;
import com.vxb.testmvp.test_zonghe.test_5.Test5Activity;
import com.vxb.testmvp.test_zonghe.test_6.Test6Activity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class TestMainActivity extends AppCompatActivity implements View.OnClickListener {

    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    @Bind(R.id.tv_test)
    TextView tv_test;
    @Bind(R.id.et_test)
    EditText et_test;
    @Bind(R.id.bt_set)
    Button bt_set;
    @Bind(R.id.bt_set_icon)
    Button bt_set_icon;
    @Bind(R.id.bt_clear)
    Button bt_clear;

    private List<String> list = new ArrayList<>();
    private RecyclerView.Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_main);

        ButterKnife.bind(this);
        initData();

        initErrorText();

        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(TestMainActivity.this).inflate(android.R.layout.simple_list_item_1, parent, false);
                return new MyViewHolder(view);
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
                MyViewHolder myViewHolder = (MyViewHolder) holder;
                myViewHolder.tv_1.setText(list.get(position));

                myViewHolder.tv_1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (position) {
                            case 0:
                                launch(TestMActivity.class);
                                break;
                            case 1:
                                launch(TestTowActivity.class);
                                break;
                            case 2:
                                launch(Test3Activity.class);
                                break;
                            case 3:
                                launch(Test4Activity.class);
                                break;
                            case 4:
                                launch(Test5Activity.class);
                                break;
                            case 5:
                                launch(Test6Activity.class);
                                break;
                        }
                    }
                });

            }

            @Override
            public int getItemCount() {
                return list == null ? 0 : list.size();
            }
        };
        recyclerView.setAdapter(adapter);

    }

    private void initErrorText() {
        bt_clear.setOnClickListener(this);
        bt_set.setOnClickListener(this);
        bt_set_icon.setOnClickListener(this);


    }

    private void initData() {
        list.add("Test1");
        list.add("Test2");
        list.add("Test3");
        list.add("Test4");
        list.add("Test5");
        list.add("Test6");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_clear:
                et_test.setError(null, null);
//          editText.setError(null);
                break;
            case R.id.bt_set:
                et_test.setError(et_test.getText());

                break;
            case R.id.bt_set_icon:
                Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
                drawable.setBounds(0, 0, 20, 20);
                et_test.setError(et_test.getText(), drawable);
                break;
        }

    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        @Bind(android.R.id.text1)
        TextView tv_1;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    protected void launch(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }


}
